#!/bin/sh
#desc:Configure music player
#package:odmusic
#type:local

CONFIGFILE="/etc/opendomo/music.conf"
VOL="0,10,20,30,40,50,60,70,80,90,100"
IFS=$'\x0A'$'\x0D'

# If the configuration file don't exist, create a default file
if ! test -e $CONFIGFILE; then
	logevent notice odmusic "No configuration file found"
	VOLUME="100"
	# Detecting music storage
	cd /media
	for i in *; do
		cd $i
		logevent debug odmusic "Searching for music files in `pwd`"
		FFILE=`find -name *.mp3 | head -n1`
		if ! test -z "$FFILE"; then
			MDIR="/media/$i/`echo $FFILE | cut -f2 -d'/'`"
			continue
		fi
		cd ..
	done
	logevent debug odmusic "Writing config file"
	# If nothing was found in the previous search...
	if test -z "$MDIR"; then 
		echo "STORAGE=/media/opendomodistro/music/" 	>  $CONFIGFILE
	else
		echo "STORAGE=$MDIR" 	>  $CONFIGFILE
	fi
	echo "ALBUM=none"				>> $CONFIGFILE
	echo "VOLUME=$VOLUME"				>> $CONFIGFILE
	echo "AUTOM3U=on"				>> $CONFIGFILE
	echo "RDM=off"					>> $CONFIGFILE
	echo "LOOP=off"					>> $CONFIGFILE
#	echo "MUSICDB=/etc/opendomo/bgmusic.db"		>> $CONFIGFILE
# Empty MUSICDB. It should be created once storage is specified
	echo "MUSICDB="					>> $CONFIGFILE
	echo "TMPFILE=/tmp/bgmusic.command"             >> $CONFIGFILE
	echo "INFFILE=/tmp/bgmusic.info"                >> $CONFIGFILE
	echo "TMPPENDING=/tmp/bgmusic.pending"          >> $CONFIGFILE
	echo "TMPPENDINGAUX=/tmp/bgmusic.pendingaux"	>> $CONFIGFILE
	echo "TMPLAST=/tmp/bgmusic.last"		>> $CONFIGFILE
	echo "TMPCURRENT=/tmp/bgmusic.current"		>> $CONFIGFILE
	echo "TMPPLAYED=/tmp/bgmusic.played" 		>> $CONFIGFILE
	echo "TMPCTLPARM=/tmp/bgmusic.ctlparam"		>> $CONFIGFILE
	echo "PID=/var/run/bgmusic.pid"                 >> $CONFIGFILE

fi

# Check if the parameter is the music folder or other parameter
if ! test -z $1; then
	if test -d "$1"; then

		# Extract configuration and modify

		. $CONFIGFILE

		# If auto db is on, create auto database
		
		if test "$AUTOM3U" == "on"; then
			/usr/local/opendomo/bgmusicManageDB.sh "create" "$1" 2>/dev/null
		fi
	
		# Save configuration

		OPTIONS=`cat "$CONFIGFILE" | grep -v STORAGE`
		echo "$OPTIONS"	 			 >$CONFIGFILE
		echo "STORAGE=\"$1\"" | tr + " "	>>$CONFIGFILE

		if test -d $1; then
			if touch $1/.music.db; then
				echo "MUSICDB=$1/.music.db" 	>>$CONFIGFILE
			else
				echo "#ERROR Database is read-only"
			fi
		fi

		if ! test -z $3; then
			OPTIONS=`cat "$CONFIGFILE" | grep -v ALBUM | grep -v VOLUME`
			echo "$OPTIONS"	 			 >$CONFIGFILE
			echo "ALBUM=\"$2\"" | tr + " "		>>$CONFIGFILE
			echo "VOLUME=$3"			>>$CONFIGFILE
		fi
		if ! test -f $1/opendomo.cfg; then
			echo "DISKTYPE=music" >> $1/opendomo.cfg
		fi
	else
		
		OPTIONS=`cat "$CONFIGFILE" | grep -v $1`
		echo "$OPTIONS"  >$CONFIGFILE
		echo "$1=$2" 	>>$CONFIGFILE
	fi
fi

# Extract albums in database and show interface

if test -z "$MUSICDB"; then
	echo "#ERROR Couldn't find music files"
	echo
	exit 
fi
. $CONFIGFILE
for album in `cat $MUSICDB | grep cover | cut -f2 -d"(" | cut -f1 -d")" | sort | uniq`; do
        if test -z $PLIST; then
       	        PLIST="$album"
       	else
               	PLIST="$PLIST","$album"
       	fi
done

echo "#> Configure music player"
echo "form:`basename $0`"
echo "	STORAGE	Music folder	text	$STORAGE"
echo "	ALBUM	Default album	list[$PLIST]	$ALBUM"
echo "	VOL	Music volume	list[$VOL]	$VOLUME"
echo

echo "#> Change default parameters"
echo "form:`basename $0`"
echo "	AUTOM3U	Organized music	subcommand[on,off]	$AUTOM3U"
echo "	RDM	Random	subcommand[on,off]	$RDM"
echo "	LOOP	Continuous mode	subcommand[on,off]	$LOOP"
echo "actions:"
echo "	browseFiles.sh	Add manual album"
echo "	musicControl.sh	Play music"
echo

